import { createApp, Directive } from 'vue'
import directives from '@/utils/directives'

import './assets/style/element-variables.scss'
import './assets/style/element/index.scss'
import './assets/font_4391505_dee4zdnwcnc/iconfont.css'
import 'element-plus/dist/index.css'

import App from './App.vue'
import routes from './router/index'
import './public-path.js'
import 'element-plus/theme-chalk/dark/css-vars.css'
import './assets/style/main.less'
import pinia from '@/stores'
import setDirectives from '@/directives/index'
import { createRouter, createWebHistory } from 'vue-router'
import { beforeEach, afterEach } from "./router/routerValid"
import { getButtonInfoById, getButtonApiUrlById } from '@/utils/tool'
// import { basePagenQuery, basePageQueryKey } from '@/hooks/usePageQuery'
import VueGridLayout from 'vue-grid-layout'

import ElementPlus from 'element-plus'
// 图标并进行全局注册
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
let instance: any = null
let router: any = null
function render(props: any = {}) {
  const { container } = props

  router = createRouter({
    // history: createWebHistory((window as any).__POWERED_BY_QIANKUN__ ? '/gm-web' : '/'),
    history: createWebHistory('/'),
    // history: createWebHistory('/rm'),
    routes,
  })
    // router = createRouter({
  //   history: createWebHistory((window as any).__POWERED_BY_QIANKUN__
  //     ? window.__POWERED_BY_QIANKUN__.context // 直接使用Qiankun提供的上下文对象里的路径
  //     : '/'), // 非Qiankun环境下使用根路径
  //   routes,
  // });
  router.beforeEach(beforeEach)
  router.afterEach(afterEach)
  instance = createApp(App)
  instance.config.globalProperties.$Button = getButtonInfoById
  instance.config.globalProperties.$Button_api = getButtonApiUrlById
  // 注册全局分页查询对象
  // instance.provide(basePageQueryKey, basePagenQuery);
 //  注册指令
 Object.keys(directives).forEach(key => {
  //Object.keys() 返回一个数组，值是所有可遍历属性的key名
  instance.directive(key, (directives as { [key: string]: Directive })[key]) //key是自定义指令名字；后面应该是自定义指令的值
})
  instance.use(router)
  instance.use(pinia)
  instance.use(setDirectives)
  instance.use(ElementPlus)
  instance.use(VueGridLayout)

  instance.mount(container ? container.querySelector('#portal') : '#portal')
  // instance.mount(container ? container.querySelector('#rm-web') : '#rm-web')
}
console.log(ElementPlusIconsVue,'duixiang');
// 注册所有图标
// for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
//   instance.component(key, component)
// }

// 独立运行时
if (!(window as any).__POWERED_BY_QIANKUN__) {
  render()
}


export async function bootstrap() {
  console.log('启动')
}
export async function mount(props: object) {
  console.log('加载', props)
  render(props)
}
export async function unmount() {
  instance.unmount()
  instance._container.innerHTML = ''
  instance = null
  router = null
}
